#! /bin/bash
source .env

function create_dir() {
  if [ ! -d "$1" ]; then
    mkdir -p $1
    chmod 777 $1
  fi
}

function start() {
  sh build.sh
  docker-compose down
  docker-compose up -d
  docker rmi $(docker images -q -f dangling=true)
}

# 保存镜像
function imageSave() {
  echo "${LINE_STR} before save image"
  if [ -f "$PWD/image.tar.gz" ]; then
    rm -rf mysql.tar.gz
  fi
  docker save ${image_tag} | gzip > image.tar.gz
  echo "${LINE_STR} after save image"
}

# 加载镜像
function imageLoad() {
  echo "${LINE_STR} before load image"
  gunzip -c image.tar.gz | docker load
  echo "${LINE_STR} after load image"
}

echo "input command: $1"
if [ "$1" == "start" ]; then
  start 
elif [ "$1" == "save" ]; then
  imageSave
elif [ "$1" == "load" ]; then
  imageLoad
else
  start
fi

echo "${LINE_STR} commands available to users: "
echo "bash startup.sh: 启动容器"
echo "bash startup.sh start: 启动容器"
echo "bash startup.sh save: 保存镜像"
echo "bash startup.sh load: 加载镜像"